package com.example.demo.hutool.redis;



import cn.hutool.db.nosql.redis.RedisDS;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPubSub;

import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class JedisSubscribe2 {

    public static Integer log;
    public static void main(String[] args) {
        extracted(30);

    }

    public static void extracted(int c) {
        log=c;
        JedisPubSub jedisPubSub = new JedisPubSub() {
            @Override
            public void onMessage(String channel, String message) {
                System.out.println("onMessage"+"---"+channel + ":" + message);
                System.out.println("log  "+log);
                System.out.println("JedisSubscribe2.log  "+JedisSubscribe2.log);
            }
            @Override
            public void onPMessage(String pattern, String channel, String message) {
              //  log.info("onPMessage订阅消息 pattern->{} channel->{} message->{}", pattern, channel, message);
                try {
                    System.out.println("onPMessage"+"---"+channel + ":" + message);
                } catch (Exception e) {
                  //  log.error("onPMessage订阅消息 pattern->{} channel->{} message->{} 发送异常->{}", pattern, channel, message, e.getMessage());
                }
            }

            @Override
            public void onSubscribe(String channel, int subscribedChannels) {
                System.out.println("onSubscribe"+"---"+channel + ":" + subscribedChannels);
            }
        };

        Executors.newScheduledThreadPool(1).scheduleWithFixedDelay(() -> {
            try(Jedis jedis =  RedisTypeTest.getJedisPool().getResource()){
                System.out.println("jedis资源获取成功");
                // 订阅频道消息
                jedis.psubscribe(jedisPubSub,"*sxm");
            } catch (Exception e) {
                System.out.println("异常，等待重试");
            }
        },0L,5L, TimeUnit.SECONDS);
    }

    //继承
    class Subscribe extends JedisPubSub{
        @Override
        public void onMessage(String channel, String message) {
            System.out.println("onMessage"+"---"+channel + ":" + message);
        }

        @Override
        public void onSubscribe(String channel, int subscribedChannels) {
            System.out.println("onSubscribe"+"---"+channel + ":" + subscribedChannels);
        }
    }
}